<template>
  <div class="product-card">
    <div class="product-image">
      <img :src="product.image" :alt="product.name" />
      <div v-if="product.featured" class="featured-badge">推荐</div>
    </div>

    <div class="product-info">
      <h3 class="product-name">{{ product.name }}</h3>
      <p class="product-description">{{ product.description }}</p>

      <div class="product-meta">
        <span class="product-price">¥{{ product.price }}</span>
        <span class="product-stock">库存: {{ product.stock }}</span>
      </div>

      <div class="product-actions">
        <button class="add-to-cart-btn" @click="addToCart" :disabled="product.stock === 0">
          {{ product.stock === 0 ? '缺货' : '加入购物车' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useCartStore } from '@/stores/cart'
import type { ProductProps } from '@/types/storeType'

const { product } = defineProps<{
  product: ProductProps
}>()

const { addItem } = useCartStore()

function addToCart() {
  // 添加到购物车逻辑
  addItem(product)
}
</script>

<style scoped>
.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
  transition:
    transform 0.3s,
    box-shadow 0.3s;
  background: white;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.featured-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: #e74c3c;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.8rem;
}

.product-info {
  padding: 1rem;
}

.product-name {
  margin: 0 0 0.5rem 0;
  font-size: 1.1rem;
  color: #333;
}

.product-description {
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 1rem;
  line-height: 1.4;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.product-price {
  font-size: 1.2rem;
  font-weight: bold;
  color: #e74c3c;
}

.product-stock {
  font-size: 0.8rem;
  color: #666;
}

.product-actions {
  display: flex;
  gap: 0.5rem;
}

.add-to-cart-btn,
.view-detail-btn {
  flex: 1;
  padding: 0.5rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  font-size: 0.9rem;
  transition: background-color 0.3s;
}

.add-to-cart-btn {
  background: #007bff;
  color: white;
}

.add-to-cart-btn:hover:not(:disabled) {
  background: #0056b3;
}

.add-to-cart-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.view-detail-btn {
  background: #f8f9fa;
  color: #333;
  border: 1px solid #ddd;
}

.view-detail-btn:hover {
  background: #e9ecef;
}
</style>
